
var mysql= require('mysql')
var express = require('express')
var unid = require('node-uuid')
var bodyParser = require('body-parser')
var app = express();
//创建数据库连接
console.log(1234567)
var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'root',
  password : '123456',
  database : 'mysql'
});
// 跨域设置
app.all('*', function (req, res, next) {
  res.header("Access-Control-Allow-Credentials", true)
  res.header("Access-Control-Allow-Origin", "*")
  res.header("Access-Control-Allow-Headers", "Content-Type,Access-Token")
  res.header("Access-Control-Allow-Methods", "*")
  res.header("X-Powered-By", ' 3.2.1')
  res.header("Content-Type", "application/json;charset=utf-8")
  next()
})
//开始连接
connection.connect();
app.use(bodyParser.json())
//全部
app.post('/all',function(request,response){
	console.log('all')
	var  sql = 'SELECT * FROM states';
	connection.query(sql,function (err, result) {
	        if(err){
	          console.log('出错');
	          console.log('[SELECT ERROR] - ',err.message);
	          return;
	        }
	        response.writeHead(200);
	        //为了看到前端加载效果设置延时
	        setTimeout(function(){response.end(JSON.stringify({
	       		status:1,
	       		list:result
	       }))},800)

	});
});
//删
app.post('/delete',function(request,response){
	console.log('delete')
	//获得请求参数
	var param = request.body;
	var  sql = 'delete from states where id ='+param.id;
	connection.query(sql,function (err, result) {
	        if(err){
		          console.log('[SELECT ERROR] - ',err.message);
		          response.end(JSON.stringify({
					       		status:-1,
					       		list:[],
					       		msg:'删除失败'
				       }))
		          return;
	        }
	       response.writeHead(200);
	       console.log(JSON.stringify(result));
	       response.end(JSON.stringify({
	       		status:1,
	       		list:result,
	       		msg:'删除成功'
	       }))
	});
});

//查
app.post('/select',function(request,response){
	console.log('select')
	//获得请求参数
	var param = request.body;
	console.log(param);

	//var  sql = 'select from states where state =' + param.state + ' or continent=' + param.continent + ' or capital=' + param.capital
	let sql = 'select * from states where';
	let params = 0;
	if(param.state){
		sql = sql + ' state="' + param.state +'"'
		params ++
	}
	if(param.continent){
		if(params ==0){
			sql = sql + ' continent="' + param.continent + '"'
		}else{
			sql = sql + ' or continent="' + param.continent + '"'
		}
		params ++
	}
	if(param.capital){
		if(params ==0){
			sql = sql + ' capital="' + param.capital + '"'
		}else{
			sql = sql + ' or capital="' + param.capital + '"'
		}
	}
	console.log(sql);
	connection.query(sql,function (err, result) {
	        if(err){
		          console.log('[SELECT ERROR] - ',err.message);
		          response.end(JSON.stringify({
					       		status:-1,
					       		list:[],
					       		msg:'查找失败'
				       }))
		          return;
	        }
	       response.writeHead(200);
	       console.log(JSON.stringify(result));
	       response.end(JSON.stringify({
	       		status:1,
	       		list:result,
	       		msg:'查询成功'
	       }))
	});
});

//增

app.post('/add',function(request,response){
  //获得请求参数
  var param = request.body;
  console.log(param);
  param.id = unid.v1();
  var addParam = [param.state,param.capital,param.continent,param.id]
  console.log(param);
  var addSql = 'INSERT INTO states(state,capital,continent,id) VALUES(?,?,?,?)';
  connection.query(addSql,addParam,function (err, result) {
    if(err){
      console.log('[SELECT ERROR] - ',err.message);
      response.end(JSON.stringify({
        status:-1,
        list:[],
        msg:'新增失败'
      }))
      return;
    }
    response.writeHead(200);
    response.end(JSON.stringify({
      status:1,
      msg:'新增成功'
    }))
  });
})

app.get('/getAll',function(request,response){
	response.writeHead(200);
	response.end('傻吊')
})
app.listen(9090)






//返回数据回调函数封装
function callback(request,response){
	//查
	var  sql = 'SELECT * FROM states';
	connection.query(sql,function (err, result) {
	        if(err){
	          console.log('出错');
	          console.log('[SELECT ERROR] - ',err.message);
	          return;
	        }
	        response.writeHead(200);
	       console.log(JSON.stringify(result));
	       response.end(JSON.stringify({
	       		status:1,
	       		list:result
	       }))
	});
}
